package com.koron.order.common.util;

import com.koron.bean.base.BaseBean;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * 检查当前登录是否具有提交权限
 */
@Component
@Aspect
public class CheckPermissionAspect {

    private EamUser getEamUser() {
        return ThreadLocalContext.get();
    }

    @Pointcut(value = "@annotation(CheckPermission)")
    public void pointcut(){

    }

    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        EamUser eamUser = getEamUser();
        Response response = (Response) joinPoint.proceed();
        Object data = response.getData();
        Class<?> aClass = data.getClass();
        if (BaseBean.class.isAssignableFrom(aClass)) {
            BaseBean baseBean = (BaseBean) data;
            if (eamUser.getAccount().equals(baseBean.getCreateBy())) {
                Field field = aClass.getDeclaredField("hasSubmitPermission");
                field.setAccessible(true);
                field.set(data, true);
            }
        }
        response.setData(data);
        return response;
    }


}
